package com.bwie.gddproject.index.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.gddproject.R;
import com.bwie.gddproject.event.MessageEvent;
import com.bwie.gddproject.event.PriceAndCountEvent;
import com.bwie.gddproject.index.adapter.ShopCarAdapter;
import com.bwie.gddproject.index.model.bean.GoodCarBean;
import com.bwie.gddproject.index.precenter.IShopCarPrecenter;
import com.bwie.gddproject.index.precenter.ShopCarPresenter;
import com.bwie.gddproject.xiangqing.view.XiangQingActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;


/**
 * Created by 我走路带风 on 2017/11/9.
 */

public class ShopCarFragment extends Fragment implements IShopCarFragment, View.OnClickListener {
    private ExpandableListView mElv;
    private CheckBox mCheckbox2;
    /**
     * 0
     */
    private TextView mTvPrice;
    /**
     * 结算(0)
     */
    private TextView mTvNum;
    private ShopCarAdapter shopCarAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.shopcart_fragment, null);
        initView(view);
        EventBus.getDefault().register(this);
        SharedPreferences user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        if (user.getString("name","")==null || user.getString("name","")== ""){
            Toast.makeText(getActivity(),"请登录您的账号...",Toast.LENGTH_SHORT).show();
        }else{
            IShopCarPrecenter iShopCarPrecenter = new ShopCarPresenter(this);
            iShopCarPrecenter.getGoods();
        }
        return view;
    }

    @Override
    public void showList(final List<GoodCarBean.DataBean> groupList, final List<List<GoodCarBean.DataBean.ListBean>> childList) {
        Log.e("tag",groupList.size()+"");
        shopCarAdapter = new ShopCarAdapter(getActivity(), groupList, childList);
        mElv.setAdapter(shopCarAdapter);
        mElv.setGroupIndicator(null);
        //默认让其全部展开
        for (int i = 0; i < groupList.size(); i++) {
            mElv.expandGroup(i);
        }
        mElv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Intent intent = new Intent(getContext(), XiangQingActivity.class);
                intent.putExtra("pid",childList.get(i).get(i1).getPid()+"");
                startActivity(intent);
                return false;
            }
        });
    }

    private void initView(View view) {
        mElv = (ExpandableListView) view.findViewById(R.id.elv);
        mCheckbox2 = (CheckBox) view.findViewById(R.id.checkbox2);
        mCheckbox2.setOnClickListener(this);
        mTvPrice = (TextView) view.findViewById(R.id.tv_price);
        mTvNum = (TextView) view.findViewById(R.id.tv_num);
        mTvNum.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.checkbox2:
                shopCarAdapter.changeAllListCbState(mCheckbox2.isChecked());
                break;
            case R.id.tv_num:
                break;
        }
    }
    @Subscribe
    public void onMessageEvent(MessageEvent event) {
        mCheckbox2.setChecked(event.isChecked());
    }

    @Subscribe
    public void onMessageEvent(PriceAndCountEvent event) {
        mTvNum.setText("结算(" + event.getCount() + ")");
        mTvPrice.setText(event.getPrice() + "");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
